package myname;

import javax.swing.*;

import java.awt.event.*;
import java.io.*;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
public class student extends JApplet{
    public String s=new String();
	private JTextField textField;
	private JButton btnNewButton,btnNewButton_1,btnNewButton_2,btnNewButton_3;
	File qingjia=new File("qingjia.txt");
	File weidao=new File("weidao.txt");
	File yidao=new File("yidao.txt");
	private ArrayList<String> names = new ArrayList<String>();
	/**
	 * Create the applet.
	 */
	public void init(){
		getContentPane().setLayout(null);
		
		SAXReader reader = new SAXReader();
		File file = new File("datas.xml");
		try {
			Document dom = reader.read(file);
			Element root = dom.getRootElement();
			Iterator<Element> ite = root.elementIterator("name");
			while (ite.hasNext()) {
				Element e = ite.next();
				names.add(e.getText());
			}
		} catch (DocumentException e) {
			e.printStackTrace();
		}
		
		
		textField = new JTextField();
		textField.setBounds(22, 20, 152, 77);
		getContentPane().add(textField);
		textField.setColumns(10);
		
		btnNewButton = new JButton("choose\r\n");
		btnNewButton.setBounds(184, 74, 93, 23);
		getContentPane().add(btnNewButton);
		
		btnNewButton_1 = new JButton("\u8BF7\u5047");
		btnNewButton_1.setBounds(211, 122, 66, 23);
		getContentPane().add(btnNewButton_1);
		
		btnNewButton_2 = new JButton("\u672A\u5230");
		btnNewButton_2.setBounds(115, 122, 66, 23);
		getContentPane().add(btnNewButton_2);
		
		btnNewButton_3 = new JButton("\u5DF2\u5230");
		btnNewButton_3.setBounds(22, 122, 66, 23);
		getContentPane().add(btnNewButton_3);
		
		btnNewButton.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent event){
				s=namechoose();
				textField.setText(s);
			}
		});
		btnNewButton_1.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent event){
				try {
					BufferedWriter q = new BufferedWriter(new FileWriter(qingjia,true));
					q.write(Data()+" "+s);
					q.newLine();
					q.close();
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
		btnNewButton_2.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent event){
				try {
					BufferedWriter w = new BufferedWriter(new FileWriter(weidao,true));
					w.write(Data()+" "+s);
					w.newLine();
					w.close();
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
		btnNewButton_3.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent event){
				try {
					BufferedWriter y = new BufferedWriter(new FileWriter(yidao,true));
					y.write(Data()+" "+s);
					y.newLine();
					y.close();
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}
	
	public String namechoose(){
		int index = (int)(Math.random() * names.size());
		String s = names.get(index);
		return s;
	}
	public String Data(){
		Date d=new Date();
		int year=d.getYear();
		int month=d.getMonth();
		month=month+1;
		year=year+1900;
		String time=year+"年"+month+"月"+d.getDate()+"日";
		return time;
	}
	
	
	
}